package com.activiti.z_six.controller;

import com.activiti.z_six.entity.dictType.DictDataEntity;
import com.activiti.z_six.entity.dictType.DictTypeEntity;
import com.activiti.z_six.entity.taskAssignee.SmsEntity;
import com.activiti.z_six.mapper.dictTypeMapper.DictEntityMapper;
import com.activiti.z_six.mapper.taskAssigneeMapper.SmsEntityMapper;
import com.activiti.z_six.service.SmsEntityService;
import com.activiti.z_six.util.AjaxResponse;
import com.activiti.z_six.util.SystemConfig;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
public class SystemConfigController {
    @Autowired
    private DictEntityMapper dictEntityMapper;
    @Autowired
    private SmsEntityService smsEntityService;
    @GetMapping("/system/dictDataList")
    public AjaxResponse dictDataList(HttpServletResponse response,
                                     @RequestParam("dictType") String dictType){
        try{
            List<DictDataEntity> dictDataList=dictEntityMapper.dictDataList(dictType);
            List<HashMap<String,Object>> hashMapList=new ArrayList<>();
            for(DictDataEntity dictData:dictDataList){
                HashMap<String,Object> hashMap=new HashMap<>();
                hashMap.put("label",dictData.getName());
                hashMap.put("value",Integer.parseInt(dictData.getId()));
                hashMapList.add(hashMap);
            }
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMapList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @GetMapping("/system/diceTypeList")
    public AjaxResponse diceTypeList(){
        try{
            List<DictTypeEntity> dictTypeList=dictEntityMapper.diceTypeList();
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), dictTypeList);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @PostMapping("/system/getSmsList")
    public AjaxResponse getSmsList(@RequestBody JSONObject param){
        HashMap<String,Object> hashMap=new HashMap<>();
        try{
            int pageNum=param.getInteger("pageno");
            int pagesize=param.getInteger("pagesize");
            String state=param.getString("status");
            String username=param.getString("username");
            int startIndex = (pageNum - 1) * pagesize;
            int maxIndex = startIndex + pagesize;
            hashMap=smsEntityService.smsList(username,startIndex,maxIndex,state);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), hashMap);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @PostMapping("/system/getSms")
    public AjaxResponse getSms(@RequestBody JSONObject param){
        try{
            String id=param.getString("id");
            SmsEntity smsEntity =smsEntityService.smsEntity(id);
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), smsEntity);
        }
        catch (Exception ex){
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), ex.getMessage());
        }
    }
    @PostMapping("/system/readSms")
    public AjaxResponse readSms(@RequestBody JSONObject param){
        String id=param.getString("id");
        String msg =smsEntityService.updateState(id);
        if(msg.equals("suess")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "执行成功");
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
    @PostMapping("/system/deleteSms")
    public AjaxResponse deleteSms(@RequestBody JSONObject param){
        String id=param.getString("id");
        String msg =smsEntityService.deleteSms(id);
        if(msg.equals("suess")) {
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.SUCCESS.getCode(),
                    SystemConfig.ResponseCode.SUCCESS.getDesc(), "执行成功");
        }
        else{
            return AjaxResponse.AjaxData(SystemConfig.ResponseCode.ERROR.getCode(),
                    SystemConfig.ResponseCode.ERROR.getDesc(), msg);
        }
    }
}
